package com.caigou.modules.famous.viewmodel

import android.text.TextUtils
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.caigou.comm.base.ext.Resource
import com.caigou.comm.base.utils.Logger
import com.caigou.modules.famous.bean.PersonalBean
import com.caigou.modules.famous.repository.FamousRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.catch
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 * @author #Suyghur.
 * Created on 2021/12/09
 */
@HiltViewModel
class PersonalViewModel @Inject constructor(private val repository: FamousRepository) : ViewModel() {

    val teXiaoListLiveData = MutableLiveData<Resource<PersonalBean>>()
    val teWeiListLiveData = MutableLiveData<Resource<PersonalBean>>()
    val pingTeListLiveData = MutableLiveData<Resource<PersonalBean>>()
    val banShuListLiveData = MutableLiveData<Resource<PersonalBean>>()
    val otherListLiveData = MutableLiveData<Resource<PersonalBean>>()
    val shaBangListLiveData = MutableLiveData<Resource<PersonalBean>>()
    val rewardLiveData = MutableLiveData<Resource<String>>()
    val buyFlowerLiveData = MutableLiveData<Resource<String>>()
    val followLiveData = MutableLiveData<Resource<String>>()

    fun getTeXiaoContestList(dareTypeId: Int, type: Int, userId: String, currentUserId: String) {
        teXiaoListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getContestList(dareTypeId, type, userId, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PersonalBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    teXiaoListLiveData.value = it
                }
        }
    }

    fun getTeWeiContestList(dareTypeId: Int, type: Int, userId: String, currentUserId: String) {
        teWeiListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getContestList(dareTypeId, type, userId, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PersonalBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    teWeiListLiveData.value = it
                }
        }
    }

    fun getPingTeContestList(dareTypeId: Int, type: Int, userId: String, currentUserId: String) {
        pingTeListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getContestList(dareTypeId, type, userId, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PersonalBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    pingTeListLiveData.value = it
                }
        }
    }

    fun getBanShuContestList(dareTypeId: Int, type: Int, userId: String, currentUserId: String) {
        banShuListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getContestList(dareTypeId, type, userId, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PersonalBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    banShuListLiveData.value = it
                }
        }
    }

    fun getOtherContestList(dareTypeId: Int, type: Int, userId: String, currentUserId: String) {
        otherListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getContestList(dareTypeId, type, userId, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PersonalBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    otherListLiveData.value = it
                }
        }
    }

    fun getShaBangContestList(dareTypeId: Int, type: Int, userId: String, currentUserId: String) {
        shaBangListLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.getContestList(dareTypeId, type, userId, currentUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.data)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<PersonalBean>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    shaBangListLiveData.value = it
                }
        }
    }

    fun setReward(type: Int, number: Int, senderId: String, receiverId: String) {
        rewardLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setReward(type, number, senderId, receiverId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    rewardLiveData.value = it
                }
        }
    }

    fun buyFlower(userId: String, number: Int) {
        buyFlowerLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.buyFlower(userId, number)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    buyFlowerLiveData.value = it
                }
        }
    }

    fun setFollow(followUserId: String, masterUserId: String) {
        followLiveData.value = Resource.Loading()
        viewModelScope.launch {
            repository.setFollow(followUserId, masterUserId)
                .map {
                    if (it.code == 0) {
                        Resource.Success(it.msg)
                    } else {
                        Resource.DataError(it.code, it.msg)
                    }
                }
                .catch {
                    val message = if (TextUtils.isEmpty(it.message)) {
                        ""
                    } else {
                        it.message!!
                    }
                    Resource.DataError<String>(-1, message)
                    Logger.e("catch: ${it.message}")
                }
                .collectLatest {
                    followLiveData.value = it
                }
        }
    }
}